class PlayerShip extends SpaceShip implements ICollidable
{
  var keysPressed;
  int shotCoolDown = 300;
  bool shotReady = true;
  int coolDownIntervalId;
  
  PlayerShip(): super(getShipAnimSpec())
  {
    this.isEnemy = false;
    this.speedX = 5;
    this.speedY = 5;
    
    setAnim("iddle");
    play(true);
    
    keysPressed = {
       "up":false,
       "down":false,
       "left":false,
       "right":false,
       "shoot":false
    };
    
    window.on.keyDown.add(this.onKeyDown);
    window.on.keyUp.add(this.onKeyUp);
    
  }
  
  static Object getShipAnimSpec()
  {
    var spec = {
        "src" : 'img/shipAnimation.png',
        "frameWidth" : 115,
        "frameHeight" : 69,
        "iddle" : [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7]]
      };
    
    return spec;
  }
  void onNewParent()
  {
    this.setPosition(this.width, (parent.height/2).toInt());
    super.onNewParent();
  }
  
  void collisionDetected()
  {
    //print("Game Over");
    //destroyed = true;
  }
  
  void onKeyUp(event)
  {
    switch(event.keyIdentifier)
    {
      //a
      case 'U+0041':
        keysPressed["left"] = false;
        break;
        
      //d
      case 'U+0044':
        keysPressed["right"] = false;
        break;
      
      //w
      case 'U+0057':
        keysPressed["up"] = false;
        break;
      
      //s
      case 'U+0053':
        keysPressed["down"] = false;
        break;
        
      //enter
      case 'Enter':
        keysPressed["shoot"] = false;
        break;
    } 
  }
  
  void onKeyDown(event)
  {
    switch(event.keyIdentifier)
    {
      //left
      case 'U+0041':
        keysPressed["left"] = true;
        break;
        
      //right
      case 'U+0044':
        keysPressed["right"] = true;
        break;
      
      //up
      case 'U+0057':
        keysPressed["up"] = true;;
        break;
      
      //down
      case 'U+0053':
        keysPressed["down"] = true;
        break;
        
      //enter
      case 'Enter':
        keysPressed["shoot"] = true;
        break;
    }

  }
  
  void update(int time)
  {    
    super.update(time);
    
    if(keysPressed["up"])
    {
      this.incY(-this.speedY);
      if(this.getY() < 0) this.setY(0);
    }
    
    else if(keysPressed["down"])
    {
      this.incY(this.speedY);
      if(this.getY() + this.height > parent.height)
      {
        this.setY(parent.height - this.height);
      }
    }
    
    if(keysPressed["right"])
    {
      this.incX(this.speedX);
      if(this.getX() + this.width > parent.width)
      {
        this.setX(parent.width - this.width);
      }
    }
    else if(keysPressed["left"])
    {
      this.incX(-this.speedX);
      if(this.getX() < 0) this.setX(0);
    }
    
    if(keysPressed["shoot"] && shotReady)
    {
      shoot();
    }
  }
  
  void startCoolDown()
  {
    shotReady = false;
    coolDownIntervalId = window.setInterval(coolDown, shotCoolDown);
  }
  
  void coolDown()
  {
    shotReady = true;
    window.clearInterval(coolDownIntervalId);
  }
  
  void shoot()
  {
    SoundManager mgr = new SoundManager();
    
    Bullet bullet = new Bullet();
    bullet.setX(this.getX() + this.width);
    bullet.setY(this.getY() + (this.height/2).toInt());
    
    this.parent.addChild(bullet);
    
    startCoolDown();
  }
  
  String getType()
  {
    return "PLAYER_SHIP";
  }
  
  List<String> getCollidableTypes()
  {
    return ["ENEMY_SHIP"]; 
  }
}
